/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 19:49
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using Orz.LogManager.Config;

namespace Orz.LogManager
{
/// <summary>
/// Description of MatchForm.
/// </summary>
public class MatchForm : System.Windows.Forms.Form
{
		private System.Windows.Forms.CheckBox chkReplace;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ComboBox cboColours;
		private System.Windows.Forms.TextBox txtMatch;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.Button cmdOK;
		private System.Windows.Forms.TextBox txtReplacement;
		private System.Windows.Forms.CheckBox chkColour;
	
	
	private logsetLogfileMatch original_i;
	private logsetLogfileMatch working_i;
	private bool bWasCancelled_i;
	
	private HelpForm help_i;
	
	public MatchForm(logsetLogfileMatch match_p)
	{
		InitializeComponent();
		
		original_i = match_p;
			
		// Make a backup of the working set before we start
		working_i = LogsetLoader.CloneLogfileMatch (original_i);
		
		help_i = new HelpForm (this);
		
		PopulateFields();
	}
	
	
		
	public bool WasCancelled
	{
		get { return bWasCancelled_i; }
	}
	
	
	
	
	
	private void PopulateFields ()
	{	
		txtMatch.Text = working_i.pattern;
		cboColours.Text = working_i.colour;
		txtReplacement.Text = working_i.replacement;
		
		chkColour.Checked = (null != working_i.colour);
		chkReplace.Checked = (null != working_i.replacement);
		
		UpdateButtons();
	}
	
	private void UpdateButtons()
	{
		cboColours.Enabled = chkColour.Checked;
		txtReplacement.Enabled = chkReplace.Checked;
	}
	
	
	private bool DoSave()
	{
		// Make sure a pattern has been defined
		if (0 == txtMatch.Text.Length)
		{
			MessageBox.Show ("You must at least specify a pattern.", this.Text);
			return false;
		}
		
		// Make sure they've specified a colour string if the checkbox is set
		if (chkColour.Checked  &&  0 == cboColours.Text.Length)
		{
			MessageBox.Show ("You should enter the name of a colour or uncheck the 'colour' checkbox.", this.Text);
			return false;
		}
		
		if (!chkColour.Checked  &&  !chkReplace.Checked)
		{
			MessageBox.Show ("What's the point of searching for something if you don't want to colour it in or replace it with something!?", this.Text);
			return false;
		}
		
		
		// Copy the values across to the working version
		original_i.pattern = txtMatch.Text;
		
		original_i.colour = (chkColour.Checked ? cboColours.Text : null);
		original_i.replacement = (chkReplace.Checked ? txtReplacement.Text : null);
								
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	
	void CmdOKClick(object sender, System.EventArgs e)
	{
		if (DoSave())
		{
			bWasCancelled_i = false;
			this.Close();
		}
	}
	
	void CmdCancelClick(object sender, System.EventArgs e)
	{
		bWasCancelled_i = true;
		this.Close();
	}
	
	void ChkColourCheckedChanged(object sender, System.EventArgs e)
	{
		UpdateButtons();
		if (! chkColour.Checked) cboColours.Text = "";
	}
	
	void ChkReplaceCheckedChanged(object sender, System.EventArgs e)
	{
		UpdateButtons();
		if (! chkReplace.Checked) txtReplacement.Text = "";
	}
	
	void CboColoursSelectedIndexChanged(object sender, System.EventArgs e)
	{
		
	}
	
	void TxtReplacementTextChanged(object sender, System.EventArgs e)
	{
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	#region Windows Forms Designer generated code
	/// <summary>
	/// This method is required for Windows Forms designer support.
	/// Do not change the method contents inside the source code editor. The Forms designer might
	/// not be able to load this method if it was changed manually.
	/// </summary>
	private void InitializeComponent() {
			this.chkColour = new System.Windows.Forms.CheckBox();
			this.txtReplacement = new System.Windows.Forms.TextBox();
			this.cmdOK = new System.Windows.Forms.Button();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.txtMatch = new System.Windows.Forms.TextBox();
			this.cboColours = new System.Windows.Forms.ComboBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label1 = new System.Windows.Forms.Label();
			this.chkReplace = new System.Windows.Forms.CheckBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkColour
			// 
			this.chkColour.Location = new System.Drawing.Point(16, 64);
			this.chkColour.Name = "chkColour";
			this.chkColour.Size = new System.Drawing.Size(184, 24);
			this.chkColour.TabIndex = 2;
			this.chkColour.Text = "Colour them in!";
			this.chkColour.CheckedChanged += new System.EventHandler(this.ChkColourCheckedChanged);
			// 
			// txtReplacement
			// 
			this.txtReplacement.Location = new System.Drawing.Point(200, 96);
			this.txtReplacement.Name = "txtReplacement";
			this.txtReplacement.Size = new System.Drawing.Size(160, 21);
			this.txtReplacement.TabIndex = 1;
			this.txtReplacement.Tag = "Any lines which match the regular expression above will have that pattern replace" +
"d with whatever you put in here.  You can use back references like $1 to refer t" +
"o anything in brackets in your pattern.";
			this.txtReplacement.Text = "";
			this.txtReplacement.TextChanged += new System.EventHandler(this.TxtReplacementTextChanged);
			// 
			// cmdOK
			// 
			this.cmdOK.Location = new System.Drawing.Point(400, 16);
			this.cmdOK.Name = "cmdOK";
			this.cmdOK.TabIndex = 6;
			this.cmdOK.Tag = "Closes this dialog box and temporarily saves the changes you have made.";
			this.cmdOK.Text = "&OK";
			this.cmdOK.Click += new System.EventHandler(this.CmdOKClick);
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(400, 48);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.TabIndex = 5;
			this.cmdCancel.Tag = "Closes this dialog box, undoing any changes you have made.";
			this.cmdCancel.Text = "&Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCancelClick);
			// 
			// txtMatch
			// 
			this.txtMatch.Location = new System.Drawing.Point(200, 32);
			this.txtMatch.Name = "txtMatch";
			this.txtMatch.Size = new System.Drawing.Size(160, 21);
			this.txtMatch.TabIndex = 1;
			this.txtMatch.Tag = "Any lines in the log file matching this text or regular expression will be replac" +
"ed with the text below, or coloured in, or both.";
			this.txtMatch.Text = "";
			// 
			// cboColours
			// 
			this.cboColours.Items.AddRange(new object[] {
						"Red",
						"Orange",
						"Yellow",
						"Green",
						"Cyan",
						"Blue",
						"Purple",
						"Pink",
						"Moccasin",
						"LightYellow",
						"LightGreen",
						"LightCyan",
						"LightBlue",
						"Plum"});
			this.cboColours.Location = new System.Drawing.Point(200, 64);
			this.cboColours.Name = "cboColours";
			this.cboColours.Size = new System.Drawing.Size(160, 21);
			this.cboColours.TabIndex = 3;
			this.cboColours.Tag = "A colour to highlight any lines which match the text or pattern above.";
			this.cboColours.Text = "Red";
			this.cboColours.SelectedIndexChanged += new System.EventHandler(this.CboColoursSelectedIndexChanged);
			this.cboColours.SelectionChangeCommitted += new System.EventHandler(this.CboColoursSelectedIndexChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cboColours);
			this.groupBox1.Controls.Add(this.chkColour);
			this.groupBox1.Controls.Add(this.txtMatch);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.txtReplacement);
			this.groupBox1.Controls.Add(this.chkReplace);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(376, 136);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(184, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Look for lines containing (regex):";
			// 
			// chkReplace
			// 
			this.chkReplace.Location = new System.Drawing.Point(16, 96);
			this.chkReplace.Name = "chkReplace";
			this.chkReplace.Size = new System.Drawing.Size(184, 24);
			this.chkReplace.TabIndex = 2;
			this.chkReplace.Text = "Replace the matching text with:";
			this.chkReplace.CheckedChanged += new System.EventHandler(this.ChkReplaceCheckedChanged);
			// 
			// MatchForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(488, 158);
			this.Controls.Add(this.cmdOK);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cmdCancel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MatchForm";
			this.ShowInTaskbar = false;
			this.Text = "Search / replace / colour";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);
		}
	#endregion
	
	
}
}
